package com.rollCall.common.config;


import com.rollCall.common.interceptor.JwtTokenAdminInterceptor;
import com.rollCall.common.interceptor.JwtTokenUserInterceptor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * 配置类，注册web层相关组件
 */
@Configuration
@Slf4j
@RequiredArgsConstructor
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

	private final JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
	private final JwtTokenUserInterceptor jwtTokenUserInterceptor;


	/**
	 * 注册自定义拦截器
	 */
    @Override
	protected void addInterceptors(InterceptorRegistry registry) {
		log.info("开始注册自定义拦截器...");
		registry.addInterceptor(jwtTokenAdminInterceptor)
				.addPathPatterns("/admin/**")
				.excludePathPatterns("/admin/login");

		registry.addInterceptor(jwtTokenUserInterceptor)
		        .addPathPatterns("/user/**")
		        .excludePathPatterns("/user/login");
	}

	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
				.allowedOriginPatterns("*")
				.allowedMethods("GET", "POST", "PUT", "DELETE")
				.maxAge(3600)
				.allowedHeaders("Origin", "Accept", "Content-Type", "Authorization", "toe")
				.allowCredentials(true);
	}
}
